<?php
/**
 * File which contains the function for converting tables
 * to a HTML form.
 *
 * @package Functions
 */

/**
 * Function to convert database-tables to a form.
 *
 * @global array $_CONFIG
 * @param string $sTable
 * @param string $sStep
 * @param string $sChooseField 
 */
function convertTableToForm ($sTable, $sStep, $sChooseField = null)
{
	global $_CONFIG;

	if ($sStep == 'options')
	{
		echo '<form method="POST" name="' . $sTable . '" action="">' . PHP_EOL;
		echo '<label for="' . $sChooseField . '">' . ucfirst ($sChooseField) . '</label><br />' . PHP_EOL . '<select name="' . $sChooseField . '"><br />' . PHP_EOL;
		$sQuery = "SELECT " . $sChooseField . " FROM " . $sTable . ";";
		$eQuery = mysql_query ($sQuery);

		while ($rResult = mysql_fetch_assoc ($eQuery))
		{
			foreach ($rResult as $sOption)
			{
				echo '	<option value="' . $sOption . '">' . $sOption . '</option>' . PHP_EOL;
			}
		}
		echo '</select>' . PHP_EOL;
		echo '<br /><br /><input name="submit" type="submit" value="Submit" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="reset" type="reset" value="Reset" />
		</form>';
	}
	elseif ($sStep == 'form')
	{
		echo '<style type="text/css">
		label
		{
			width: 110px;
			float: left;
		}
		</style>
		<form method="POST" name="' . $sTable . '" action="">';
		$sInfoQuery = "SELECT COLUMN_NAME, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" . $_CONFIG['pPDO']['db'] . "' AND TABLE_NAME = '" . $sTable . "';";
		$eInfoQuery = mysql_query ($sInfoQuery);

		if (isset ($sChooseField) && !empty ($sChooseField))
		{
			$sDataQuery = "SELECT * FROM " . $sTable . " WHERE " . $sChooseField . " = '" . $_POST[$sChooseField] . "';";
			$eDataQuery = mysql_query ($sDataQuery);
			$rDataResult = mysql_fetch_assoc ($eDataQuery);
		}

		while ($rInfoResult = mysql_fetch_assoc ($eInfoQuery))
		{
			$sColumnName = $rInfoResult ['COLUMN_NAME'];

			if ($sColumnName != $sTable . '_id')
			{
				$sColumnType = str_replace (')', '', str_replace (' unsigned', '', $rInfoResult ['COLUMN_TYPE']));
				$sColumnType = str_replace ('\'', '', $sColumnType);
				$aColumnInfo = explode ('(', $sColumnType);

				echo '<label for="' . $sColumnName . '">' . ucfirst ($sColumnName) . '</label>';

				if ($aColumnInfo [0] == 'int' || $aColumnInfo [0] == 'varchar')
				{
					$sType = ($sColumnName == 'password') ? 'password' : 'text';
					$sValue = (isset ($sChooseField) && !empty ($sChooseField) && $sType != 'password') ? 'value="' . $rDataResult[$sColumnName] . '"' : '';
					//$bDisable = ($sColumnName == 'email') ? 'disabled = "disabled"' : '';

					echo '<input maxlength="' . $aColumnInfo[1] . '" name="' . $sColumnName . '|' . $aColumnInfo[1] . '" size="20" type="' . $sType . '" id="' . $sColumnName . '" ' . $sValue . ' />' . '<br />' . PHP_EOL;
				}

				if ($aColumnInfo [0] == 'text')
				{
					$sValue = (isset ($sChooseField) && !empty ($sChooseField)) ? $rDataResult[$sColumnName] : '';

					echo '<textarea cols="17" name="' . $sColumnName . '|' . $aColumnInfo[1] . '" rows="5" id="' . $sColumnName . '" /></textarea><br />' . PHP_EOL;
				}

				if ($aColumnInfo [0] == 'enum')
				{
					$sValues = str_replace ('\'', '', $aColumnInfo [1]);
					$aValues = explode (',', $sValues);

					echo '<select name="' . $sColumnName . '|' . $aColumnInfo[1] . '" id="' . $sColumnName . '"><br />' . PHP_EOL;
					foreach ($aValues as $sEnum)
					{
						$sValue = (isset ($sChooseField) && !empty ($sChooseField) && $sEnum == $rDataResult[$sColumnName]) ? 'selected="selected"' : '';
						echo '	<option value="' . $sEnum . '" ' . $sValue . '>' . $sEnum . '</option>' . PHP_EOL;
					}
					echo '</select>' . PHP_EOL;
				}
			}
		}

		echo '<br /><br /><input name="submit" type="submit" value="Submit" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="reset" type="reset" value="Reset" />
		</form>';
	}
	elseif ($sStep == 'submit')
	{
		$bError = false;

		foreach ($_POST as $sField => $sValue)
		{
			$sFieldInfo = explode ('|', $sField);
			if ($sFieldInfo[0] == 'password' && !empty ($sValue))
			{
				if (isset ($sFieldInfo[1]) && is_numeric ($sFieldInfo[1]) && (strlen ($sValue) <= 0 || strlen ($sValue) > $sFieldInfo[1] || !$sValue))
				{
					echo 'Er is iets mis met het volgende veld: ' . $sFieldInfo[0] . '<br />' . PHP_EOL;
					$bError = true;
				}
			}
		}

		if ($bError === false)
		{
			$sInsertQuery = "INSERT INTO `" . $sTable . "` (";

			foreach ($_POST as $sField => $sValue)
			{
				$sField = explode ('|', $sField);
				$sField = $sField[0];

				if ($sField == 'password' && empty ($sValue))
				{

				}
				else
				{
					if ($sField != 'submit')
					{
						$sInsertQuery .= "`" . $sField . "`, ";
					}
				}
			}

			$sInsertQuery .= ')';
			$sInsertQuery = str_replace (', )', ') VALUES (', $sInsertQuery);


			foreach ($_POST as $sField => $sValue)
			{
				$sField = explode ('|', $sField);
				$sField = $sField[0];

				if ($sField == 'password' && empty ($sValue))
				{

				}
				else
				{
					if ($sField != 'submit')
					{
						$sValue = ($sField == 'password') ? sha1 ('just to make it salt it thingie' . md5 ($sValue)) : $sValue;
						$sInsertQuery .= "'" . $sValue . "', ";
					}
				}
			}

			$sInsertQuery .= ')';
			$sInsertQuery = str_replace (', )', ') ON DUPLICATE KEY UPDATE ', $sInsertQuery);

			foreach ($_POST as $sField => $sValue)
			{
				$sField = explode ('|', $sField);
				$sField = $sField[0];

				if ($sField == 'password' && empty ($sValue))
				{

				}
				else
				{
					if ($sField != 'submit')
					{
						$sValue = ($sField == 'password') ? sha1 ('just to make it salt it thingie' . md5 ($sValue)) : $sValue;
						$sInsertQuery .= "`" . $sField . "`='" . $sValue . "', ";
					}
				}
			}

			$sInsertQuery = substr ($sInsertQuery, 0, -2);
			$sInsertQuery .= ';';
		}
		elseif ($bError === true)
		{
			echo 'U moet eerst iets aan de bovenstaande foutmeldingen doen, voordat het verder verwerkt kan worden.';
		}
		else
		{
			echo 'Error';
		}
	}
	else
	{
		echo 'WTF are you trying to pull here...!? GTFO plx.';
	}
}